package com.example.wan1.view.homenew;

import android.util.Log;

import com.example.wan1.data.BaseObserver;
import com.example.wan1.data.RetrofitUtil;
import com.example.wan1.data.RootObserver;
import com.example.wan1.data.RxSchedulers;
import com.example.wan1.model.ArticleBean;
import com.example.wan1.model.BannerBean;
import com.example.wan1.model.common.CommonResBody;
import com.example.wan1.model.common.CommonResList;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.functions.BiFunction;

/***
 * 创建时间：2020/7/1 21:02
 * 创建人：10850
 * 功能描述：
 * 问题描述：当我们刚进入界面的请求和下拉刷新的请求一致时，该如何优雅的处理？
 */
public class NewHomePresenter implements
        NewHomeContract.Presenter {

    @Inject
    NewHomeContract.View mView;

    @Inject
    public NewHomePresenter() {

    }

    private List<ArticleBean> mList;

    /***
     * 最新项目 刚进入
     */
    public void start() {
        mList = new ArrayList<>();
        getArticleList(0);
    }

    @Override
    public void getArticleList(int page) {
        Observable.zip(RetrofitUtil.getInstance().getArticleList(page), RetrofitUtil.getInstance().getProjectNewList(page),
                new BiFunction<CommonResBody<CommonResList<ArticleBean>>, CommonResBody<CommonResList<ArticleBean>>, List<ArticleBean>>() {
                    @Override
                    public List<ArticleBean> apply(CommonResBody<CommonResList<ArticleBean>> commonResListCommonResBody, CommonResBody<CommonResList<ArticleBean>> commonResListCommonResBody2) throws Exception {
                        List<ArticleBean> datas = commonResListCommonResBody.getData().getDatas();
                        List<ArticleBean> list = commonResListCommonResBody2.getData().getDatas();
                        datas.addAll(list);
                        return datas;
                    }
                }).compose(RxSchedulers.observableIO2Main())
                .subscribe(new RootObserver<ArticleBean>() {
                    @Override
                    public void onSuccess(List<ArticleBean> result) {
                        mView.onSuccessArticle(result);
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        mView.onFailure(e.getMessage());
                    }
                });

        /*//最新文章
        RetrofitUtil.getInstance().getArticleList(page)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new BaseObserver<CommonResList<ArticleBean>>() {
                    @Override
                    public void onSuccess(CommonResList<ArticleBean> result) {
                        mView.onSuccess(result);
                        mList.addAll(result.getDatas());
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        mView.onFailure(e.getMessage());
                    }
                });
        //最新项目
        RetrofitUtil.getInstance().getProjectNewList(page)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new BaseObserver<CommonResList<ArticleBean>>() {
                    @Override
                    public void onSuccess(CommonResList<ArticleBean> result) {
                        mView.onSuccess(result);
                        mList.addAll(result.getDatas());
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        mView.onFailure(e.getMessage());
                    }
                });*/
    }

    @Override
    public void getBannerList() {
        RetrofitUtil.getInstance().getBannerList()
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new BaseObserver<List<BannerBean>>() {
                    @Override
                    public void onSuccess(List<BannerBean> result) {
                        mView.onSuccessBanner(result);
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                    }
                });
    }
}
